<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    title="更新授权"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel">
    <template slot="footer">
      <a-button key="close" @click="handleCancel">{{$t('m.common.button.cancel')}}</a-button>
      <a-button key="ok" type="primary" @click="handleSubmit">{{ $t('m.common.button.ok') }}</a-button>
    </template>

    <a-form-model ref="form" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="特征码" prop="mac">
        {{form.mac}}
      </a-form-model-item>
      <a-form-model-item label="授权码" prop="lic">
        <a-input v-model="form.lic" type="textarea"/>
      </a-form-model-item>
    </a-form-model>

  </a-modal>
</template>

<script>

  import { getAction, postAction } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'UpdateLic',
    data () {
      return {
        visible: false,
        confirmLoading: false,
        form: {},
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        rules: {
          lic: [{ required: true, message: '必填!' }],
        },
        url: {
          queryMacStr: YchConstant.PREFIX_SYSTEM + '/sysconfig/queryMacStr',
          updateConfigLic: YchConstant.PREFIX_SYSTEM + '/sysconfig/updateConfigLic',
        }
      }
    },
    methods: {
      show () {
        this.visible = true
        this.confirmLoading = true
        getAction(this.url.queryMacStr).then((res) => {
          if (res.success) {
            this.form = {
              mac: res.result
            }
          } else {
            this.messageUtil.warning(res.message)
          }
        }).catch(() => {
          this.messageUtil.warning('查询数据失败')
        }).finally(() => {
          this.confirmLoading = false
        })
      },
      handleCancel () {
        this.visible = false
      },
      handleSubmit () {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.updateLic()
          }
        })
      },
      updateLic () {
        this.confirmLoading = true
        postAction(this.url.updateConfigLic, this.form.lic).then((res) => {
          if (res.success) {
            this.messageUtil.success('更新成功，重启系统后生效')
            this.handleCancel()
            this.$emit('afterUpdateLic')
          } else {
            this.messageUtil.warning(res.message)
          }
        }).catch(() => {
          this.messageUtil.warning('操作失败')
        }).finally(() => {
          this.confirmLoading = false
        })
      }
    }
  }
</script>
